package com.two.screen.water.balance.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.two.screen.water.balance.R
import com.two.screen.water.balance.guard.LocalService
import com.two.screen.water.balance.guard.RemoteService
import com.two.screen.water.balance.permission.PermissionUtil
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.ui.service.AppService
import com.two.screen.water.balance.utils.ARouterUtils
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.ZXingUtils
import com.two.screen.water.balance.utils.eventbus.Event
import com.two.screen.water.balance.utils.eventbus.EventCode
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_allow_app.*

@Route(path = ARouterUtils.AllowAppActivity)
class AllowAppActivity : BaseActivity() {
    override fun getContentLayout(): Any = R.layout.activity_allow_app

    private var mUniqueId: String? = null

    companion object {
        var mIsShowAllow = false
    }

    override fun beforeSetContentView() {
        App.flag = 0
    }

    override fun initView(savedInstanceState: Bundle?) {
        //加密 uuid
        mUniqueId = CommUtils.getAppUUid()
        mAllowIv.setImageBitmap(ZXingUtils.createQRCodeBitmap(mUniqueId, 2000))
        mAppUnitId.text = mUniqueId
        mVersionNameTv.text = CommUtils.getAppVersionName()
        //mAllowIv.postDelayed({ mTempStatues = 406 }, 5000)
    }

    override fun initData() {
        PermissionUtil.requestPermission(
            this, arrayOf(
                //感觉有些系统阉割个GPS,要不要考虑把下面定位干点。
                Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION,
                //com.yanzhenjie.permission.Permission.READ_PHONE_STATE,
                Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE
            ), { }
        )
        //ImageManger.setBgImageCenterCrop(this, "", mAllowBgIv)
    }

    @SuppressLint("CheckResult")
    override fun initListener() {
        mAllowIv.setOnClickListener {
            ARouter.getInstance()
                .build(ARouterUtils.StartUseActivity)
                .navigation()
            finish()
        }
    }

    override fun isRegisterEventBus(): Boolean = true
    override fun receiveEvent(event: Event<Any>) {
        super.receiveEvent(event)
        when (event.code) {
            EventCode.FINISH_ALLOW_SCREEN -> goBack()
        }
    }

    override fun goBack() {
//        ARouter.getInstance()
//            //.build(ARouterUtils.BtlChooseActivity)
//            .navigation()
        super.goBack()
    }

    override fun onResume() {
        super.onResume()
        mIsShowAllow = true
    }

    override fun onDestroy() {
        super.onDestroy()
        mIsShowAllow = false
    }

    override fun exit(): Boolean {
        try {
            if (isExit) {
                stopService(Intent(this, AppService::class.java))
                stopService(Intent(this, RemoteService::class.java))
                stopService(Intent(this, LocalService::class.java))
            }
        } catch (e: Exception) {
            d("退出失败:$e")
        }
        return super.exit()
    }

    override fun doubleQuit(): Boolean = true
}